<?php
/**
 * User: Nicho
 * Date: 17-3-31
 * Time: AM 08:10
 */

namespace Admin\Controller;


class AttendanceController extends AdminController
{
    const MODEL_NAME = 'Attendance@:';

    //接口列表
    public function index()
    {   

        $map['status'] = array('neq','-1');
        $model = D('core_attendance'); //存入数据库中，进行排序并显示页面
        $list = $model->where($map)->limit($limit)->order('id desc')->select();
        $this->assign('_list',$list);
        $this->display(self::MODEL_NAME.'Index/index'); 
        
    }
       public function addrecord()
    {
        if(IS_POST){
            $a=strtotime(I('createtime'));
            $b=strtotime(I('endDate'));
            $arr=array($a,$b);
            $dd['leave_date']=implode(",",$arr);
            $dd['create_time']=time();
            $dd['unit']=I('unit');
            $dd['due_num']=I('due_num');
            $dd['miss_num']=I('miss_num');
            $dd['uid']=I('uid');
            $d=M('core_attendance')->add($dd);
            $this->success('添加成功',U('index'));
        }else{
            $typeList = M('user_role')->select();
            
            $this->assign('typeList',$typeList);
            $this->meta_title = '新增接口';
            $this->display(self::MODEL_NAME.'Index/addrecord');   
        }

    }
    //接口类型
    public function type()
    {   
        if(I('keyword')){
            $keyword = I('keyword');
            $map['title'] = array('like','%'.$keyword.'%');
            $this->assign('keyword',$keyword);
        }
        if(I('sort')){
            $sort = I('sort');
            if($sort == 'recycle'){
                $map['status'] = -1;

                $this->assign('recycle','【回收站】');
            }
        }else{
            $sort = 'normal';
            $map['status'] = array('egt', 0);
        }
        $model = D('CoreInterfaceType');
        $totalCount = $model->where($map)->count();
        $page = new \Think\Page($totalCount,10);
        $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $list = $model->where($map)->limit($limit)->order('sort,id desc')->select();
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->assign('sort',$sort);
        $this->meta_title = '接口类型';
        $this->display(self::MODEL_NAME.'Type/index'); 
        
    }
     public function addType()
    {
        if(IS_POST){
            if(!I('post.title')) $this->error('请输入名称!');
            $model_object = D('CoreInterfaceType');
            $model_object->title = trim(I('post.title'));
            $model_object->remark = trim(I('post.remark'));
            $model_object->status = 1;
            $model_object->create_time = NOW_TIME;
            if($model_object->add()){
                $this->success('新增成功!',U('type'));
            }else{
                $this->error('新增失败!');
            }
        }

        
        $this->meta_title = '新增类型';
        $this->display(self::MODEL_NAME.'Type/addtype');

    }
    public function editType()
    {
        if(IS_POST){
            $id = I('post.id');
            $model_object = D('core_attendance');
            $model_object->due_num = trim(I('post.due_num'));
            $model_object->miss_num = trim(I('post.miss_num'));
            $model_object->update_time = NOW_TIME;
            $model_object->id = $id;
            if($model_object->save()){
                $this->success('更新成功!',U('index'));
            }else{
                $this->error('更新失败!');
            }
        }
        $id = I('get.id');
        $model_object = D('core_attendance');
        $data = $model_object->find($id);
        $this->assign('data',$data);
        $this->meta_title = '编辑类型';
        $this->display(self::MODEL_NAME.'Index/bianji');

    }

    //设置状态
     public function setStatus()
    {
        $ids = I('request.id');
        $model = I('request.model');
        if(!$ids)$this->error('无可操作数据!');
        $map['id'] = array('in',$ids);
        $method = I('request.method');
        $model_object = D($model);
        switch ($method) {
            case 'resume':
                $model_object->status = 1;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('激活成功!');
                }else{
                    $this->error('激活失败!');
                }
                break;
            case 'forbid':
                $model_object->status = 2;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('禁用成功!');
                }else{
                    $this->error('禁用失败!');
                }
                break;
            case 'delete':
                $model_object->status = -1;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('删除成功!');
                }else{
                    $this->error('删除失败!');
                }
                break;
            case 'recover':
                $model_object->status = 0;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('恢复成功!');
                }else{
                    $this->error('恢复失败!');
                }
                break;
            case 'destory':
                $model_object->status = -2;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('彻底删除成功!');
                }else{
                    $this->error('彻底删除失败!');
                }
                break;
            default:
                $this->error('系统参数错误!');
                break;
        }
    }
}
